package Accessors;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import utils.RESTfulConnect;
import utils.URLTools;

import Callbacks.AbstractCallback;
import Enums.RFAT;
import Models.BarrelList;
import Models.Barrels;
import Presenter.BarrelListPresenter;
import Presenter.BarrelsPresenter;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

@SuppressWarnings("rawtypes")
public class BarrelsAccessor extends AbstractAccessor<Barrels,BarrelsPresenter,AbstractCallback> {
	private final String NAME = "barrels";
	private final String DIALOG = "Barrel";
	
	private final static BarrelsPresenter presenter = new BarrelsPresenter();
	
	AbstractCallback callback;
	
	//Modal dialog variable.
	ProgressDialog modalDialog = null;
	
	public BarrelsAccessor(AbstractCallback callback){
		super(presenter, callback);
		this.callback = callback;
	}
	
	//Public access functions.
	public void getAllItems(final Context context){
		String dialog = "Loading " + DIALOG + " List...";
		String rfString = NAME;
		super.getAllItems(context, dialog, rfString);
	}
	
	//Public access functions.
	public void getFilledBarrels(final Context context){
		String dialog = "Loading " + DIALOG + " List...";
		String rfString = NAME + "/runNamedQuery/Barrels.findFilled";
		super.getAllItems(context, dialog, rfString);
	}
	
	public void getItem(final int id, final Context context){
		String dialog = "Loading " + DIALOG + "...";
		String rfString = NAME + "/" + Integer.toString(id);
		super.getItem(context, dialog, rfString);
	}
	
	public void doMovementToBarrels(final int barID, final int brewID, final int employeeID
            , final double volume, final Date date, final Context context){
		String dialog = "Moving To " + DIALOG + "...";
		String rfString = NAME + "/doMovementToBarrel/" + Integer.toString(barID) + "/"
	            + Integer.toString(brewID) + "/" + Integer.toString(employeeID) + "/"
	            + Double.toString(volume) + "/" + date.toString();
		rfString = rfString.replaceAll(" ", "%20");
		super.getItem(context, dialog, rfString);
	}
	
	public void doItemUpdate(final Barrels item, final Context context){
		String dialog = "Updating " + DIALOG + "...";
		String rfString = NAME;
		super.doItemUpdate(item, context, dialog, rfString);
	}
	
	public void doItemInsert(final Barrels item, final Context context){
		String dialog = "Inserting " + DIALOG + "...";
		String rfString = NAME;
		super.doItemInsert(item, context, dialog, rfString);
	}
	
	public void doMoveBarrelsToVessel(final List<Barrels> barrels, final int newVesselID
			, final int employeeID, final Date date, final Context context){
		modalDialog = ProgressDialog.show(context, null, "Moving From Barrels To Vessel...");
        BarrelList bl = new BarrelList();
        bl.setBarrelCollection(barrels);
		final String strStart = URLTools.getURLString(context);
		String str = NAME + "/moveBarrelsToVessel/" + Integer.toString(newVesselID) + "/"
	            + Integer.toString(employeeID) + "/" + date.toString();
		str = str.replaceAll(" ", "%20");
		BarrelListPresenter presenter = new BarrelListPresenter();
		new DoMoveBarerlsToVessel().execute(strStart+str, URLTools.getUSERString(context), URLTools.getPASSString(context), presenter.createJSON(bl).toString());
	}
	
	private class DoMoveBarerlsToVessel extends AsyncTask<String, Void, String> {
    	@Override
        protected String doInBackground(String... urls) {
            try {
                return RESTfulConnect.postResource(urls[0], urls[1], urls[2], urls[3]);
            } catch (IOException e) {
            	e.printStackTrace();
            	return "Connection Error";
            }
        }
    	@SuppressWarnings("unchecked")
		@Override
        protected void onPostExecute(String result) {
    		ArrayList<BarrelList> items = new ArrayList<BarrelList>();
    		BarrelList item = new BarrelList();
    		modalDialog.dismiss();
    		callback.callback(item, RFAT.BARRELMOVETOVESSEL_COMPLETE, items);
        }
    }
}
